package com.hh.bss.util;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DBUtil {
	private static final int DEFAULT_DIV_SCALE = 10;
	public static void aveXjTotal(String firstColumn,List records,Map sumFields,String tagName){
		Map totalRecord=new LinkedHashMap();
		if (records!=null&&records.size()>0){
			Map one=(Map)records.get(0);
			for(Iterator it=one.keySet().iterator();it.hasNext();){
		    	String key=(String)it.next();
		    	
		    	if (sumFields.containsKey(key)) {
			    		totalRecord.put(key,new Double(0));	
		    	}else{		    	    	
			    		totalRecord.put(key,null);	
		    	}

		    }
		}
		
		//统计行
		for(int i=0;records!=null&&i<records.size();i++){
			Map one=(Map)records.get(i);
		    for(Iterator it=sumFields.keySet().iterator();it.hasNext();){
		    	String key=(String)it.next();
		    	Object value=one.get(key);
		    	
		    	Double v2=(Double)totalRecord.get(key);
		    	if (v2!=null)totalRecord.put(key, new Double(v2 +ObjectUtils.Object2Double(value)));
	    		
		    }
		}
		//平均
		if (records!=null&&records.size()>0){			
		    int total=records.size();
			for(Iterator it=totalRecord.keySet().iterator();it.hasNext();){
			    Object key=it.next();
			    Double v=(Double)totalRecord.get(key);
			    if (v!=null&&v.doubleValue()!=0){
			    	totalRecord.put(key, new Double(round(v.doubleValue()/total,2)));
			    }
			}
		}
		//设置tag
		Object v2=totalRecord.get(firstColumn);
		if (v2!=null) totalRecord.put(firstColumn, tagName+":"+v2);
		else totalRecord.put(firstColumn, tagName+":");
		records.add(totalRecord);	
	}
	public static void sumXjTotal(String firstColumn,List records,Map sumFields,boolean isXjOnly,boolean isTotalOnly){
		sumXjTotal( firstColumn,records,sumFields,isXjOnly,isTotalOnly,"Total",false);
	}
	public static void sumXjTotal(String firstColumn,List records,Map sumFields,boolean isXjOnly,boolean isTotalOnly,String tagName,boolean isAve){
		Map xjRecord=new LinkedHashMap();
		Map totalRecord=new LinkedHashMap();
		//初始化统计行
		int cl=0;
		boolean firstColumnHas=false;
		//String firstColumn="";
		if (records!=null&&records.size()>0){
			Map one=(Map)records.get(0);
			for(Iterator it=one.keySet().iterator();it.hasNext();){
		    	String key=(String)it.next();
		    	//Object value=one.get(key);
		    	cl++;
		    	//if (cl==1) firstColumn=key;
		    	if (sumFields.containsKey(key)) {
		    		if (cl==1) 
		    			firstColumnHas=true;
		    		
		    			xjRecord.put(key,new Double(0));
			    		totalRecord.put(key,sumFields.get(key));	
		    		
		    		
		    	}else{
		    	    	xjRecord.put(key,null);
			    		totalRecord.put(key,null);	
		    	}

		    }
		}
		//统计行
		for(int i=0;records!=null&&i<records.size();i++){
			Map one=(Map)records.get(i);
		    for(Iterator it=sumFields.keySet().iterator();it.hasNext();){
		    	String key=(String)it.next();
		    	Object value=one.get(key);
		    	
		    	Double v2=(Double)xjRecord.get(key);
		    	if (v2!=null) xjRecord.put(key, new Double(v2 +ObjectUtils.Object2Double(value)));
		    	
		    }
		}
		//算平均
		int total=records.size();

		if (isAve){
			if (records!=null&&records.size()>0){			
			    
				for(Iterator it=xjRecord.keySet().iterator();it.hasNext();){
				    Object key=it.next();
				    Double v=(Double)xjRecord.get(key);
				    if (v!=null&&v.doubleValue()!=0){
				    	
				    	xjRecord.put(key, new Double(round(v.doubleValue()/total,2)));
				    }
				}
			}
		}
		
		//设置小计，合计字眼
		if (isXjOnly){
			Object v1=xjRecord.get(firstColumn);
			if (v1!=null) xjRecord.put(firstColumn, tagName+":"+v1);
			else xjRecord.put(firstColumn, tagName+":");			
			//增加小计
			records.add(xjRecord);
			
		}else {
				
			if (isTotalOnly){
				//合计行
				Object v2=totalRecord.get(firstColumn);
				if (v2!=null) totalRecord.put(firstColumn, tagName+":"+v2);
				else totalRecord.put(firstColumn, tagName+":");
								
				records.add(totalRecord);
			}else {
				Object v1=xjRecord.get(firstColumn);
				if (v1!=null) xjRecord.put(firstColumn, "小计:"+v1);
				else xjRecord.put(firstColumn, "小计:");
				
				Object v2=totalRecord.get(firstColumn);
				if (v2!=null) totalRecord.put(firstColumn, tagName+":"+v2);
				else totalRecord.put(firstColumn, tagName+":");
				//增加小计，合计行
				records.add(xjRecord);
				records.add(totalRecord);	
			}
			
		}
	}
	//特殊的计算
  public static void sumJobSumXjTotal(String firstColumn,List records,Map sumFields,boolean isXjOnly,boolean isTotalOnly,String tagName,boolean isAve){
		
		Map xjRecord=new LinkedHashMap();
		Map totalRecord=new LinkedHashMap();
		//初始化统计行
		int cl=0;
		boolean firstColumnHas=false;
		//String firstColumn="";
		if (records!=null&&records.size()>0){
			Map one=(Map)records.get(0);
			for(Iterator it=one.keySet().iterator();it.hasNext();){
		    	String key=(String)it.next();
		    	//Object value=one.get(key);
		    	cl++;
		    	//if (cl==1) firstColumn=key;
		    	if (sumFields.containsKey(key)) {
		    		if (cl==1) 
		    			firstColumnHas=true;
		    		
		    			xjRecord.put(key,new Double(0));
			    		totalRecord.put(key,sumFields.get(key));	
		    		
		    		
		    	}else{
		    	    	xjRecord.put(key,null);
			    		totalRecord.put(key,null);	
		    	}

		    }
		}
		//统计行
		//dso的总数需要单独计算，没有的不算
		int dosTotal=0;
		for(int i=0;records!=null&&i<records.size();i++){
			Map one=(Map)records.get(i);
		    for(Iterator it=sumFields.keySet().iterator();it.hasNext();){
		    	String key=(String)it.next();
		    	Object value=one.get(key);
		    	
		    	Double v2=(Double)xjRecord.get(key);
		    	if (v2!=null) xjRecord.put(key, new Double(v2 +ObjectUtils.Object2Double(value)));
		    	if ("dso".equals(key)){
		    		try{
		    			Double dd=Double.valueOf(value.toString()).doubleValue();
		    			if (dd!=null) dosTotal++;
		    		}catch(Exception ex){}
		    	}
	    		
		    }
		}
		//算平均
		int total=records.size();
		//计算总行和
		
		
		//if (isCountExceptNull){
//			for(Iterator it=records.keySet().iterator();it.hasNext();){
//			    Object key=it.next();
//			    Double v=(Double)xjRecord.get(key);
//			    if (v!=null&&v.doubleValue()!=0){
//			    	
//			    	xjRecord.put(key, new Double(round(v.doubleValue()/total,2)));
//			    }
//			}
		//}
		if (isAve){
			if (records!=null&&records.size()>0){			
			    
				for(Iterator it=xjRecord.keySet().iterator();it.hasNext();){
				    Object key=it.next();
				    Double v=(Double)xjRecord.get(key);
				    if (v!=null&&v.doubleValue()!=0){
				    	
				    	xjRecord.put(key, new Double(round(v.doubleValue()/total,2)));
				    	if ("dso".equals(key))xjRecord.put(key, new Double(round(v.doubleValue()/dosTotal,2)));
				    	
				    }
				}
			}
		}
		
		//设置小计，合计字眼
		if (isXjOnly){
			Object v1=xjRecord.get(firstColumn);
			if (v1!=null) xjRecord.put(firstColumn, tagName+":"+v1);
			else xjRecord.put(firstColumn, tagName+":");			
			//增加小计
			records.add(xjRecord);
			
		}else {
				
			if (isTotalOnly){
				//合计行
				Object v2=totalRecord.get(firstColumn);
				if (v2!=null) totalRecord.put(firstColumn, tagName+":"+v2);
				else totalRecord.put(firstColumn, tagName+":");
								
				records.add(totalRecord);
			}else {
				Object v1=xjRecord.get(firstColumn);
				if (v1!=null) xjRecord.put(firstColumn, "小计:"+v1);
				else xjRecord.put(firstColumn, "小计:");
				
				Object v2=totalRecord.get(firstColumn);
				if (v2!=null) totalRecord.put(firstColumn, tagName+":"+v2);
				else totalRecord.put(firstColumn, tagName+":");
				//增加小计，合计行
				records.add(xjRecord);
				records.add(totalRecord);	
			}
			
		}
		
		
			
			
		
	}

	/*
	  public final String getQueryFromPreparedStatement(PreparedStatement preparedQuery, Object[] values) {   
          int len = preparedQuery.length();   
          StringBuffer t = new StringBuffer(len * 2);   
             
          if (values != null) {   
              int i = 1, limit = 0, base = 0;   
                 
              while ((limit = preparedQuery.indexOf('?',limit)) != -1) {   
                  if (isString[i]) {   
                      t.append(preparedQuery.substring(base,limit));   
                      t.append("'");   
                      t.append(values[i]);   
                      t.append("'");   
                  } else {   
                      t.append(preparedQuery.substring(base,limit));   
                      t.append(values[i]);   
                  }   
                  i++;   
                  limit++;   
                  base = limit;   
              }   
              if (base < len) {   
                  t.append(preparedQuery.substring(base));   
              }   
          }   
             
          return t.toString();   
      }  	  
	*/  
  public static double round(final double v, final int scale){
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, ((scale < 0) ? DEFAULT_DIV_SCALE : scale), BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
